#pragma once
#include <iostream>
#include <pthread.h>
#include "MyMutex.hpp"


namespace CondValArea
{
    using namespace MyMutexArea;
    using namespace CondValArea;
    class MyCondVal
    {
    public:
    MyCondVal()
    {
        int n = ::pthread_cond_init(&_cond,nullptr);
        if(n!=0)
        {
            std::cerr<<"pthread_cond_init error"<<std::endl;
            exit(1);
        }
    }

    void PthreadWait(MyMutex& mutex)//等待
    {
        int n = ::pthread_cond_wait(&_cond,&mutex.GetMutex());
        if(n!=0)
        {
            std::cerr<<"pthread_cond_wait error"<<std::endl;
            exit(-1);
        }
    }

    void PthreadSignal()//唤醒
    {
        int n = ::pthread_cond_signal(&_cond);
        if(n!=0)
        {
            std::cerr<<"pthread_cond_siganl error"<<std::endl;
            exit(-1);
        }
    }

    ~MyCondVal()
    {
        int n = ::pthread_cond_destroy(&_cond);
        if(n!=0)
        {
            std::cerr<<"pthread_cond_destroy error"<<std::endl;
            exit(1);
        }
    }
    private:
        pthread_cond_t _cond;//条件变量
    };
};